package core.model.mail;

import java.io.Serializable;

public class Message implements Serializable, Comparable {
    private String file;
    private int turn;
    private String from;
    private String body;
    private MessageType type;

    public MessageType getType() {
        return type;
    }

    public void setType(MessageType type) {
        this.type = type;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getFile() {
        return file;
    }

    public void setFile(String file) {
        this.file = file;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public int getTurn() {
        return turn;
    }

    public void setTurn(int turn) {
        this.turn = turn;
    }

    @Override
    public int compareTo(Object o) {

        if (o == null || !(o instanceof Message)) {
            throw new IllegalArgumentException("Object should be not null and instance of 'Message'");
        }

        Message message = (Message) o;

        if (this.getTurn() > message.getTurn()) {
            return -1;
        }

        if (this.getTurn() == message.getTurn()) {
            return 0;
        }

        if (this.getTurn() < message.getTurn()) {
            return 1;
        }

        return 0;
    }

    public enum MessageType {

        BROADCAST("broadcasts"),
        PRIVATE("messages");

        private String elementId;

        MessageType(String elementId) {
            this.elementId = elementId;
        }

        MessageType() {
        }

        public String getElementId() {
            return elementId;
        }

        public void setElementId(String elementId) {
            this.elementId = elementId;
        }

        public static MessageType generateType(String type) {
            if (type == null) {
                return null;
            }

            if (type.equalsIgnoreCase(PRIVATE.getElementId())) {
                return PRIVATE;
            }

            if (type.equalsIgnoreCase(BROADCAST.getElementId())) {
                return BROADCAST;
            }

            return null;
        }
    }
}